﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Creventive.Wpf.ShellFactory.Workspace.Primitives
{
    /// <summary>
    /// Represents an object designed to present workspace items.
    /// </summary>
    public class WorkspaceItemsPresenter : ItemsControl
    {
        /// <summary>
        /// Determines if the specified item is (or is eligible to be) its own container.
        /// </summary>
        /// <param name="item">The item to check.</param>
        /// <returns>
        /// true if the item is (or is eligible to be) its own container; otherwise, false.
        /// </returns>
        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return false;
        }

        /// <summary>
        /// When overridden in a derived class, undoes the effects of the <see cref="M:System.Windows.Controls.ItemsControl.PrepareContainerForItemOverride(System.Windows.DependencyObject,System.Object)"/> method.
        /// </summary>
        /// <param name="element">The container element.</param>
        /// <param name="item">The item.</param>
        protected override sealed void ClearContainerForItemOverride(DependencyObject element, object item)
        {
            ((WorkspaceItemPresenter)element).WorkspaceItem = null;
            base.PrepareContainerForItemOverride(element, item);
        }

        /// <summary>
        /// Prepares the specified element to display the specified item.
        /// </summary>
        /// <param name="element">Element used to display the specified item.</param>
        /// <param name="item">Specified item.</param>
        protected override sealed void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            ((WorkspaceItemPresenter)element).WorkspaceItem = (WorkspaceItem)item;
            base.PrepareContainerForItemOverride(element, item);
        }

        /// <summary>
        /// Creates or identifies the element that is used to display the given item.
        /// </summary>
        /// <returns>
        /// The element that is used to display the given item.
        /// </returns>
        protected override sealed DependencyObject GetContainerForItemOverride()
        {
            return new WorkspaceItemPresenter();
        }
    }
}